<script setup lang="ts">
import comIcon from '../common/com-icon/com-icon.vue';
import { ref } from 'vue';
import { postLoginBindingAPI, postLoginByCodeAPI, postLoginThirdpartyAPI } from '@/api/login';
import type { AxiosResponse } from 'axios';
import type { CodeType } from '@/types/user';
import { showToast } from 'vant';
import { useRouter } from 'vue-router';
const login = ref<boolean>(false)
let openId = ref<string>('')
// @ts-ignore
let flag = QC.Login.check()
if (flag) {
  // @ts-ignore
  QC.Login.getMe(async (id: string) => {
    openId.value = id
    await postLoginThirdpartyAPI(openId.value, 'qq').then(res => {
      console.log(res);
      if (res.data.code === 10000) {
        showToast({
          message: "登录成功"
        })
        router.push('/index')
      }

    }).catch(err => {
      if (err.code === 10001) {
        login.value = true
      }
    })
  })
}
const pattern: RegExp = /^1\d{10}$/
const mobile = ref('')
const code = ref('')
const validator = (val: string) => {
  return val.trim() !== ''
}
const onFailed = () => {
  console.log(123);

}
type Data<T> = {
  code: number;
  message: string;
  data: T;
};
const codeBtn = ref<string>('获取验证码')
const getCode = async () => {
  if (codeBtn.value !== '获取验证码') return
  let time = 60

  let res: AxiosResponse<Data<CodeType>> = await postLoginByCodeAPI(mobile.value, 'bindMobile')
  if (res.data.code === 10000) {
    showToast({
      message: "验证码为：" + res.data.data.code
    })
    code.value = res.data.data.code + ''
    setInterval(() => {
      time--
      codeBtn.value = time + 's'
      if (time <= 0) {
        codeBtn.value = '获取验证码'
      }
    }, 1000);
  }
}
const binding = async () => {
  let res = await postLoginBindingAPI(mobile.value, code.value, openId.value)
  console.log(res);
  if (res.data.code === 10000) {
    showToast({
      message: "绑定成功",
    })
    router.push('/index')
  }
}
const router = useRouter()
const back = () => {
  router.push('/login')
}
</script>

<template>
  <div class="login_Box" v-if="login">
    <!-- 导航栏 -->
    <div class="navigation_Bar">
      <comIcon name="icons-user-arrow" class="iconBack" @click="back"></comIcon>
    </div>
    <header>
      <h3>绑定手机号</h3>
    </header>
    <div style="margin: 0 10px;">
      <van-form @failed="onFailed">
        <van-cell-group>
          <div class="item">
            <van-field class="ipt" v-model="mobile" name="pattern" placeholder="请输入手机号"
              :rules="[{ pattern, message: '请输入正确的手机号' }]" />
          </div>
          <!-- 验证码 -->
          <div class="item">
            <van-field class="ipt" v-model="code" name="validator" placeholder="请输入验证码"
              :rules="[{ validator, message: '请输入正确的验证码' }]" />
            <div @click="getCode" class="codeBtn">
              {{ codeBtn }}
            </div>
          </div>
        </van-cell-group>
        <div class="buttonBox" style="margin: 16px;">
          <van-button @click="binding" round block class="submit_button" native-type="submit">
            立即绑定
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>
<style lang="scss">
.login_Box {
  width: 100%;
  height: 100%;

  .navigation_Bar {
    width: 100%;
    height: 46px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgb(248, 249, 249);

    .iconBack {
      width: 30px;
      height: 30px;
      padding: 0 16px;
      font-weight: 400;
      transform: rotate(180deg);
    }

    .register {
      font-size: 15px;
      color: rgb(22, 194, 163);
      padding: 0 16px;
    }
  }

  header {
    display: flex;
    padding: 30px 30px 50px;
    align-items: end;
    justify-content: space-between;

    h3 {
      font-size: 24px;
      font-weight: 400;
      margin: 0;
    }
  }

  .item {
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f5f6f7;

    .password-icon {
      position: absolute;
      right: 20px;
      top: 20px;
      width: 18px;
      height: 18px;
    }

    .ipt {
      padding: 14px 16px;
    }

    .codeBtn {
      position: absolute;
      right: 8px;
      top: 18px;
      color: #16c2a3;
    }
  }

  .green {
    color: #16c2a3;
    margin: 0 5px
  }

  .forgot {
    color: #3c3e42;
  }

  .after {
    .checked {
      display: flex;
      justify-content: space-between;
      margin: 10px 16px 0;
    }
  }

  .after::after {
    border: none;
  }


  .buttonBox {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

    .submit_button {
      width: 315px;
      background-color: #16c2a3;
      font-size: 16px;
      color: #fff;
    }

    a {
      color: #16c2a3;
      margin-top: 10px;

    }
  }

  .other {
    margin-top: 60px;
    padding: 0 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    .font {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      margin: 20px 0;
      color: #969799;
    }

    .font::before {
      content: "";
      background-color: #f6f7f8;
      display: block;
      flex: 1;
      box-sizing: border-box;
      height: 1px;
    }

    .font::after {
      content: "";
      display: block;
      flex: 1;
      box-sizing: border-box;
      background-color: #f6f7f8;
      height: 1px;
    }

    .other_login {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;

      .QQ {
        width: 44px;
        height: 44px;
      }
    }
  }


}
</style>
